package com.ticket.sass.admin.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ticket.sass.admin.business.MemberCardLevelBusiness;
import com.ticket.sass.admin.dto.input.member.MemberCardLevelAddInput;
import com.ticket.sass.admin.dto.input.member.MemberCardLevelListInput;
import com.ticket.sass.admin.dto.input.member.MemberCardLevelUpdInput;
import com.ticket.sass.admin.dto.output.member.MemberCardLevelListOutput;
import com.ticket.sass.admin.util.EscapeUtil;
import com.ticket.sass.common.entity.tenant.MemberCardLevelEntity;
import com.ticket.sass.admin.model.PageModel;
import com.ticket.sass.admin.service.impl.MemberCardLevelServiceImpl;
import com.ticket.sass.admin.util.ModelUtil;
import com.ticket.sass.common.exception.enums.ErrorCode;
import com.ticket.sass.common.exception.exception.BadRequestException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.TypeToken;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ywb
 * @date 2024-03-18 17:52
 */
@Slf4j
@Service
@AllArgsConstructor
public class MemberCardLevelBusinessImpl implements MemberCardLevelBusiness {

    final ModelUtil modelUtil;
    final MemberCardLevelServiceImpl levelService;
    
    @Override
    public PageModel<MemberCardLevelListOutput> levelList(MemberCardLevelListInput input) {
        List<MemberCardLevelEntity> list = levelService.list(input);
        List<MemberCardLevelListOutput> outputs = modelUtil.listToList(
            list,
            new TypeToken<List<MemberCardLevelListOutput>>(){}.getType()
        );
        return new PageModel<>((long) list.size(), outputs);
    }
    
    @Override
    public void add(MemberCardLevelAddInput input) {
        // 检查名称是否唯一
        if (levelService.count(new QueryWrapper<MemberCardLevelEntity>().eq("name", input.getName())) > 0) {
            throw new BadRequestException(
                ErrorCode.BUSINESS_ERROR,
                EscapeUtil.errorMessage("name", "名称已存在")
            );
        }
        
        // 检查等级是否唯一
        if (levelService.count(new QueryWrapper<MemberCardLevelEntity>().eq("level", input.getLevel())) > 0) {
            throw new BadRequestException(
                ErrorCode.BUSINESS_ERROR,
                EscapeUtil.errorMessage("level", "等级已存在")
            );
        }
        
        levelService.save(modelUtil.mapToMap(input, MemberCardLevelEntity.class));
    }
    
    @Override
    public void upd(MemberCardLevelUpdInput input) {
        
        // 检查名称是否唯一（排除当前记录）
        if (levelService.count(new QueryWrapper<MemberCardLevelEntity>().eq("name", input.getName()).ne("id", input.getId())) > 0) {
            throw new BadRequestException(
                ErrorCode.BUSINESS_ERROR,
                EscapeUtil.errorMessage("name", "名称已存在")
            );
        }
        
        // 检查等级是否唯一（排除当前记录）
        if (levelService.count(new QueryWrapper<MemberCardLevelEntity>().eq("level", input.getLevel()).ne("id", input.getId())) > 0) {
            throw new BadRequestException(
                ErrorCode.BUSINESS_ERROR,
                EscapeUtil.errorMessage("level", "等级已存在")
            );
        }
        levelService.updateById(modelUtil.mapToMap(input, MemberCardLevelEntity.class));
    }
}
